package com.jyxd.web.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    /**
     * 判断字符串是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^-?\\d+$");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 判断字符串是否是小数
     *
     * @param str
     * @return
     */
    public static boolean isDecimals(String str) {
        Pattern pattern = Pattern.compile("[+-]?[0-9]+(\\.[0-9]{1,4})?");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否为汉字
     *
     * @param name
     * @return
     */
    public static boolean checkName(String name) {
        int n = 0;
        for (int i = 0; i < name.length(); i++) {
            n = (int) name.charAt(i);
            if (!(19968 <= n && n < 40869)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 去除非数字
     *
     * @param str
     * @return
     */
    public static String wipeOffChinese(String str) {
        Long longStr = new Long(str.replaceAll("[^\\d]+", ""));
        return String.valueOf(longStr);
    }
}
